<?php
if(!empty($_POST)){
	require('connect.php');
	$desde = $_POST['from'];
	$hasta = $_POST['to'];
	$desde = substr($desde, 6,4)."-".substr($desde, 0,2)."-".substr($desde, 3,2);
	$hasta = substr($hasta, 6,4)."-".substr($hasta, 0,2)."-".substr($hasta, 3,2);
	$tank = $_POST['tank'];
	switch($tank) {
		case 'CLR DSL':
			$tankid='BCD8091EBFE8483AA7749DC7CF66C23C';
			break;
		case 'GASOLINE 87 OCT E-10 UNLEADED':
			$tankid='F2858F8E8A93474698407F080CD6B061';
			break;
		case 'GASOLINE 93 OCT E-10 HITEST':
			$tankid='CA7200DE3FFD4C24BC7BC2C5F6EBD787';
			break;
	}
	$arrayDatos = array();
	$nuevafecha = date ( 'Ymd' ,strtotime ( '+1 day' , strtotime($hasta)));

	$fdesde = str_replace("-", "", $desde);
	$fhasta = str_replace("-", "", $nuevafecha);

	$mes1 = date('M Y',strtotime($desde));
	$mes2 = date('M Y',strtotime($hasta));
	$final = date('j M Y',strtotime($hasta));
	$mismo_mes = false;
	//To know if the report envolves only the same month
	if($mes1==$mes2){
		$mismo_mes = true;
	}
	$query = "SELECT ID as Id, 
		DAY(TrnDate) as 'Day of Month',
		YEAR(TrnDate) as 'Year',
		ReadTime as 'Time of Read',
		OpenGal as 'Opening Gallons',
		ProdNo as 'Product',
		Water as 'Water',
		Sales as 'Sales',
		GalDeliv as 'Gross Deliveries',
		SUBSTRING(CONVERT(VARCHAR(11), TrnDate, 113), 4, 8) as 'Date',
		CONVERT(VARCHAR(8), TrnDate, 24) as 'Hour',
		Comments as 'Comments'
		FROM EnvironmentalDaily
		where CONVERT(VARCHAR(8), TrnDate, 112)   >= '$fdesde'
		and  CONVERT(VARCHAR(8), TrnDate, 112)  <= '$fhasta'
		and TankID = '$tankid'
		order by TrnDate asc";

	$rows = sqlsrv_query( $conn, $query);
	$arrayfechas = array();	
	$registros = array();

	while( $row = sqlsrv_fetch_array( $rows, SQLSRV_FETCH_ASSOC) ) {		
		$registros[] = $row;
	}
	foreach($registros as $row) {
		if(!in_array($row['Date'],$arrayfechas)){
			$arrayfechas[]= $row['Date'];
		}
	}
	if($mismo_mes){
		$arrayfechas = array();
		$arrayfechas[] = $mes1 ;
	}

	$arrayMovimientos = array();
	$cantidad = count ($registros);
	$k=0;
	foreach($registros as $row) {
		$inventory = $row["Opening Gallons"]-$row["Sales"] +$row["Gross Deliveries"];
		$siguiente = $registros [$k+1];
		$daily = $siguiente["Opening Gallons"]-$inventory;

	 if($row["Day of Month"]==1){
	 	$monthly_over = $daily;
	 } else {
		$pasado = $arrayMovimientos[$k-1];
		$monthly_over = $pasado['monthly']+$daily;
	}
	if($cantidad==$k+1){
		$pasado = $arrayMovimientos[$k-1];
		$monthly_over = $pasado['monthly'];
		$daily = 0;
	}
	$arrayMovimientos[]= array('id' => $row["Id"], 'day' => $row["Day of Month"], 'time'=> $row["Time of Read"],'hour'=>$row["Hour"],
	'opening'=> $row["Opening Gallons"], 'product'=> $row["Product"],'water'=> $row["Water"], 'sales'=>$row["Sales"],
	'gross'=>$row["Gross Deliveries"],'book'=>$inventory,'daily'=>$daily,'monthly'=>$monthly_over,'Date'=>$row["Date"],
	'comments'=>$row["Comments"]);
	$k++;
}
	foreach($arrayfechas as $fecha) {
		$movimientos = array();
		foreach($arrayMovimientos as $row) {
			if($row['Date']==$fecha) {
				$movimientos[]=$row;
			}
		}
		$arrayDatos[] = array('fecha'=>$fecha,'movimientos'=>$movimientos);
	}
	sqlsrv_close($conn);
}

if(!empty($arrayDatos)){
	foreach($arrayDatos as $dato){	
?>
	<div class="row">
	  <div class="col-xs-3">
	    <h5>STATION NAME:</h5>
	  </div>
	  <div class="col-xs-3">
	   <h5></h5>
	  </div>
	</div>
	<div class="row">
	  <div class="col-xs-3">
	    <h5>MONTH:</h5>
	  </div>
	  <div class="col-xs-3">
	   <h5><?php echo substr($dato['fecha'], 0,3);?></h5>
	  </div>
	</div>
	<div class="row">
	  <div class="col-xs-3">
	    <h5>SIZE (GALLONS):</h5>
	  </div>
	  <div class="col-xs-3">
	   <h5></h5>
	  </div>
	</div>
	<div class="row">
	  <div class="col-xs-3">
	    <h5>YEAR:</h5>
	  </div>
	  <div class="col-xs-3">
	   <h5><?php echo substr($dato['fecha'], 3);?></h5>
	  </div>
	</div>
	<div class="row">
	  <div class="col-xs-3">
	    <h5>MANIFOLDED ?</h5>
	  </div>
	  <div class="col-xs-3">
	   <h5>Yes or No</h5>
	  </div>
	</div>

<table class="table table-striped">
	<thead>
	  <tr>
	  	<th>Edit</th>
	    <th>Day of <br />Month</th>
	    <th>Hour Read</th>
	    <th>Time Of Read</th>
	    <th>Opening Tank Monitor <br />Reading In Gallons</th>
	    <th>Product</th>
	    <th>Water</th>
	    <th>Sales</th>
	    <th>Gross <br />Deliveries</th>
	    <th>Book Inventory</th>
	    <th>Daily Overs/Shorts<br /> Tomorrow's</th>
	    <th>Monthly <br />Overs/Shorts</th>
	    <th>Comments</th>
	  </tr>
	</thead>
	<tbody>
<?php 
$k=0;
//calculos
$sales =0;
$monthly_total =0;
$cantidad = count($dato['movimientos']);
$j=0;
foreach ($dato['movimientos'] as $row) {
 	$final = date('Ymd',strtotime($fhasta));
 	$fecha = date('Ymd',strtotime($row["day"]." ".$row["Date"]));
	if($final > $fecha){
		$sales +=$row["sales"];
		echo "<tr><td><a class='edit' href='#' data-id='" . $row["id"] . "' data-toggle='modal' data-backdrop='static' data-target='#modal-edit'><span class='glyphicon glyphicon-pencil'></span></a></td><td>" . $row["day"] . "</td>
		<td>".$row["hour"] . "</td>
		<td>".$row["time"] . "</td>
		<td>". $row["opening"] . "</td><td>". $row["product"] . "</td>
		<td>".$row["water"] . "</td><td>" . $row["sales"] . "</td><td>".$row["gross"] . "</td>
		<td>" . $row["book"] . "</td><td>" . $row["daily"] . "</td>
		<td>" . $row["monthly"] . "</td><td>" . $row["comments"] . "</td></tr>"; 
		if($cantidad==$j+1){
			$pasado = $dato['movimientos'][$j];
			$monthly_total = $pasado['monthly'];
		}
	}
	$j++;
}
 echo "<tr><td colspan='3'><b>Total Gallons Sold</b></td><td>$sales</td><td></td><td></td><td></td><td></td><td colspan='2'><b>Total Gallons Over/Short</b></td>
 <td colspan='1'>$monthly_total</td><td>&nbsp;</td></tr>"; 
 $leak = ($sales*0.01)+130;
 echo "<tr><td colspan='12'><b>Leak Check: 1% of total gallons sold +130 gals =</b> $leak</td></tr>";

?>
</tbody>
</table>

<?php } }?>
